<template>
  <div class="app-container">
    <el-button-group>
       <el-button type="primary" icon="el-icon-arrow-left" v-on:click="prePage">返回</el-button> 
    </el-button-group>

    <div class="demo-input-suffix">
       标题： <el-input v-model="input" placeholder="请输入标题" class="headerInput"></el-input>
    </div>

    <div class="demo-input-suffix">
       &nbsp;图标： 
    <el-upload
     :headers="header"
    style='marginTop:5px;margin-bottom:10px;margin-left:60px;'
  class="avatar-uploader"
  action="/user/uploadImg"
  :show-file-list="false"
  :on-success="handleAvatarSuccess"
  :before-upload="beforeAvatarUpload"
  v-loading="loading"
  >
  
  <img v-if="imageUrl" :src="imageUrl" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
   
  </div>
  
   <quill-editor ref="myTextEditor"
              v-model="content"
              :config="editorOption"
              @blur="onEditorBlur($event)"
              @focus="onEditorFocus($event)"
              @ready="onEditorReady($event)"
              @change="onEditorChange($event)"
            
             
              >
        <div id="toolbar" slot="toolbar">
           <!-- <button class="ql-bold">上传图片</button>
           <button class="ql-italic">Italic</button> -->
         </div>
</quill-editor>


<el-dialog title="添加图片" :visible.sync="dialogTableVisible">
 
<el-upload
  class="upload-demo"
  drag
  action="/user/uploadImg"
  :headers="header"
  accept="*"
  :on-success="onSuccess"
  >
  <i class="el-icon-upload"></i>
  <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
  <div class="el-upload__tip" slot="tip">只能上传jpg/png文件</div>
  
</el-upload>
 <div slot="footer" class="dialog-footer">
    <el-button @click="dialogTableVisible = false">取 消</el-button>
    <!-- <el-button type="primary" @click="dialogTableVisible = false">确 定</el-button> -->
  </div>
</el-dialog>

   <el-button type="success" v-on:click="update" v-loading="loading" class="addBtn">添加</el-button>

  </div>
</template>

<style  lang="scss" scope>
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
.headerInput{
  width: 30%;
  margin: 20px 0 30px;
}
  .addBtn{
    margin-top:20px;
  }
  .quill-code {
    border: none;
    height: auto;
    >code {
      width: 100%;
      margin: 0;
      padding: 1rem;
      border: 1px solid #ccc;
      border-top: none;
      border-radius: 0;
      height: 10rem;
      overflow-y: auto;
      resize: vertical;
    }
  }

  .quill-editor{
    width: 80%;
    // height: 400px;
  }
  .ql-editor{
    height: 300px;
  }

</style>
<script>
  import {
    createWork
  } from '@/api/table'
  import { getConfig } from '@/utils/common.js'
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { getCsrfToken, getToken } from '../../utils/auth.js'
export default {
    data() {
      return {
        input: null,
        work: null,
        loading: false,
        imageUrl: '',
        header: {
          'x-csrf-token': getCsrfToken(),
          'token': getToken(),
          length: '',
          baseUrl: ''
        },
        dialogTableVisible: false,
        content: '',
        editorOption: {
          modules: {
            toolbar: [
              ['bold', 'italic', 'underline', 'strike'],
              ['blockquote', 'code-block'],
              [{ 'header': 1 }, { 'header': 2 }],
              [{ 'list': 'ordered' }, { 'list': 'bullet' }],
              [{ 'script': 'sub' }, { 'script': 'super' }],
              [{ 'indent': '-1' }, { 'indent': '+1' }],
              [{ 'direction': 'rtl' }],
              [{ 'size': ['small', false, 'large', 'huge'] }],
              [{ 'header': [1, 2, 3, 4, 5, 6, false] }],
              [{ 'font': [] }],
              [{ 'color': [] }, { 'background': [] }],
              [{ 'align': [] }],
              ['clean'],
              ['link', 'image', 'video']
            ]
          }
        }
      }
    },
    created() {
    },
    mounted() {
      this.baseUrl = getConfig().baseUrl
      this.$refs.myTextEditor.quill.getModule('toolbar').addHandler('image', this.imgHandler)
    },
    methods: {
      handleAvatarSuccess(res, file) {
        this.loading = false
        console.log('handleAvatarSuccess')
        // console.log('success', res, file)
        // this.imageUrl = URL.createObjectURL(file.raw)
        // console.log("file.url",res.url)
        this.imageUrl = getConfig().baseUrl + res.url
      },
      beforeAvatarUpload(file) {
        this.loading = true
        // const isJPG = file.type === 'image/jpeg'
        // const isLt2M = file.size / 1024 / 1024 < 2

        // if (!isJPG) {
        //   this.$message.error('上传头像图片只能是 JPG 格式!')
        // }
        // if (!isLt2M) {
        //   this.$message.error('上传头像图片大小不能超过 2MB!')
        // }
        // return isJPG && isLt2M
      },
      prePage() {
        this.$router.back()
      },
      open4(msg, type = 'warning', title = '警告') {
        this.$notify({
          title: title,
          message: msg,
          type: type,
          onClose: () => {
            msg === '添加成功' ? this.$router.back() : null
          }
        })
      },
      update(data) {
        this.fullscreenLoading = true
        const obj = {
          img: this.imageUrl
        }
        if (this.input) {
          obj.title = this.input
        } else {
          this.open4('请添加标题')
          return
        }
        if (this.content.length > 0) {
          obj.work = this.content
        } else {
          this.open4('请添加内容')
          return
        }
        createWork(obj).then((e) => {
          console.log('ob++++')
          console.log(e)
          this.open4('添加成功', 'success', '消息')
          this.fullscreenLoading = false
        }).catch((e) => {
          console.log('error')
          console.log(e)
          this.open4('添加失败', 'error', '消息')
          this.fullscreenLoading = false
        })
      },
      imgHandler(e) {
        this.dialogTableVisible = true
        console.log(e)
        console.log('imgHandler')
      },
      onSuccess(e) {
        const url = getConfig().baseUrl + e.url
        const quill = this.$refs.myTextEditor.quill
        this.length = quill.getLength()
  
        quill.insertEmbed(10, 'image', url)
        console.log('value')
        console.log(this.$refs.myTextEditor.quill.value)
        this.dialogTableVisible = false
      },
      onEditorBlur() {

      },
      onEditorFocus() {

      },
      onEditorReady() {
  
      },
      onEditorChange({ editor, html, text }) {

      }
    },
    components: {
      quillEditor
    }
  }
</script>
